package com.iot08.api.service;

import com.iot08.api.response.CallRecordResp;
import com.iot08.api.vo.request.CallRecordQueryVo;
import com.iot08.common.entity.app.CallRecord;
import com.iot08.common.response.Response;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author cwj
 * @description
 * @date 2025/3/4 15:44
 **/
@Slf4j
@Service
public class CallRecordService extends BaseService<CallRecord> {

    /**
     * @description 查询通话记录信息
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/4 16:05
     **/
    public ResponseMap queryCallRecord(CallRecordQueryVo vo) {
        ResponseMap res = ResponseMap.success();
        if(ObjectUtils.isEmpty(vo.getPage())){
            vo.setPage(0);
        }
        if(ObjectUtils.isEmpty(vo.getNum())){
            vo.setNum(20);
        }
        //查询用户开始绑定设备后，设备接收到的短信信息，绑定之前的信息不让其看见
        Criteria criteria = Criteria.where("imei").is(vo.getImei());
        Query query = new Query(criteria);
        long total = mongoTemplate.count(query,"call_record");
        List<CallRecordResp> callRecordResps = mongoTemplate.find(query.with(Sort.by(Sort.Order.desc("callStartTimestamp"))).skip((long) vo.getPage() *vo.getNum()).limit(vo.getNum()),CallRecordResp.class,"call_record");
        res.put("total",total);
        res.put("callRecordResps",callRecordResps);
        return res;
    }

    /**
     * @description 清除设备通话记录
     * @author cwj
     * @param [imei]
     * @return void
     * @date @time 2025/6/24 11:42
     **/
    public void delByImei(String imei){
        Query query = new Query(Criteria.where("imei").is(imei));
        mongoTemplate.remove(query,CallRecord.class);
    }

}
